package com.twu.biblioteca;

/**
 * Created by sravanns on 19/06/14.
 */
public class LoginAsUser implements Login {
    Library library;
    UIApplication uiApplication;
    public LoginAsUser(Library library, UIApplication uiApplication) {
        this.library=library;
        this.uiApplication=uiApplication;
    }

    @Override
    public String toString() {
        return "Login as User";
    }

    @Override
    public ResultLogin performAction() throws InterruptedException {
        Result result = new Result();
        User user = null;
        //String username=uiApplication.acceptUserName();
        uiApplication.display("enter library number");
        String librayNumber=uiApplication.acceptLibraryNumber();
        uiApplication.display("enter password");
        String password=uiApplication.acceptPassword();
       // String email=uiApplication.acceptEmail();
       // String phone=uiApplication.acceptPhone();

        ResultLogin resultLogin=new ResultLogin();
        if(librayNumber.length()==8 && librayNumber.charAt(3)=='-') {
             user= library.getRegisteredMembers().get(librayNumber);
            if (user.getPassword().equals(password))
            // if(user.equals(new User(username,email,phone,password)))
            {
                result.setResult("Logged in as User", true);
                resultLogin.setResultLogin(librayNumber, result);
            } else {
                result.setResult("Invalid credentials", false);
                resultLogin.setResultLogin(librayNumber, result);
            }
        }

        else {
            result.setResult("Logged in as User", true);
            resultLogin.setResultLogin(librayNumber, result);
        }
            return resultLogin;
    }
}
